package com.wiim.ui;

import com.wiim.common.Utilities;
import com.wiim.nhapnhap.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Demonstrates the use of custom animations in a FragmentTransaction when
 * pushing and popping a stack.
 */
public class FragmentCustomAnimations extends FragmentActivity {
	int mStackLevel = 1;
	FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_stack);

		// Watch for button clicks.

		// Do first time initialization -- add initial fragment.
		Fragment newFragment = IntroOne.newInstance();

		ft.add(R.id.simple_fragment, newFragment).commit();
	}

	public static class IntroOne extends Fragment {
		static IntroOne newInstance() {
			IntroOne f = new IntroOne();

			return f;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.intro_one, container, false);
			Button tvSkip = (Button) v.findViewById(R.id.tvSkip);
			MarginLayoutParams marginParams = new MarginLayoutParams(tvSkip.getLayoutParams());
			marginParams.setMargins((int) (Utilities.getGlobalVariable(getActivity()).device_width / 13), (int) (Utilities.getGlobalVariable(getActivity()).device_height / 2.1), 0, 0);
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
			tvSkip.setLayoutParams(layoutParams);
			
			
//			String mystring = tvSkip.getText().toString();
//			SpannableString content = new SpannableString(mystring);
//			content.setSpan(new UnderlineSpan(), 0, mystring.length(), 0);
//			tvSkip.setText(content);
			((Button) tvSkip).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					// Instantiate a new fragment.
					Fragment newFragment = IntroTwo.newInstance();
					FragmentTransaction ft = getFragmentManager().beginTransaction();
					ft.setCustomAnimations(R.animator.enter, R.animator.exit, R.animator.popenter, R.animator.popexit);
					ft.replace(R.id.simple_fragment, newFragment);
					ft.addToBackStack(null);
					ft.commit();

				}
			});
			return v;
		}
	}

	public static class IntroTwo extends Fragment {
		static IntroTwo newInstance() {
			IntroTwo f = new IntroTwo();
			return f;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.intro_two, container, false);
			Button tvSkip = (Button) v.findViewById(R.id.tvSkip);
			MarginLayoutParams marginParams = new MarginLayoutParams(tvSkip.getLayoutParams());
			marginParams.setMargins((int) (Utilities.getGlobalVariable(getActivity()).device_width / 13), (int) (Utilities.getGlobalVariable(getActivity()).device_height / 2.3), 0, 0);
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
			tvSkip.setLayoutParams(layoutParams);
//			String mystring = tvSkip.getText().toString();
//			SpannableString content = new SpannableString(mystring);
//			content.setSpan(new UnderlineSpan(), 0, mystring.length(), 0);
//			tvSkip.setText(content);
			tvSkip.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					Fragment newFragment = IntroThree.newInstance();
					FragmentTransaction ft = getFragmentManager().beginTransaction();
					ft.setCustomAnimations(R.animator.enter, R.animator.exit, R.animator.popenter, R.animator.popexit);
					ft.replace(R.id.simple_fragment, newFragment);
					ft.addToBackStack(null);
					ft.commit();

				}
			});
			return v;
		}
	}

	public static class IntroThree extends Fragment {
		static IntroThree newInstance() {
			IntroThree f = new IntroThree();
			return f;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			View v = inflater.inflate(R.layout.intro_three, container, false);
			Button btnOk = (Button) v.findViewById(R.id.btnOk);
			MarginLayoutParams marginParams = new MarginLayoutParams(btnOk.getLayoutParams());
			marginParams.setMargins((int) (Utilities.getGlobalVariable(getActivity()).device_width / 13), (int) (Utilities.getGlobalVariable(getActivity()).device_height / 2.3), 0, 0);
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
			btnOk.setLayoutParams(layoutParams);
			btnOk.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					getActivity().finish();

					final Intent itTab = new Intent().setClass(getActivity().getApplicationContext(), LoginActivity.class);
					getActivity().startActivity(itTab);

				}
			});
			return v;
		}
	}

}
